import { _decorator, Component, Node, ScrollBar, ScrollView, UITransform, Label, Button, Color, ProgressBarComponent, SpriteFrame, Texture2D, Sprite } from 'cc';
import { EventMgr } from '../../../Framework/Scripts/Managers/EventMgr';
import { poolManager } from '../../../Framework/Scripts/Managers/poolManager';
import { ResMgr } from '../../../Framework/Scripts/Managers/ResMgr';
import { SoundMgr } from '../../../Framework/Scripts/Managers/SoundMgr';
import { UIComponent, UIMgr } from '../../../Framework/Scripts/Managers/UIMgr';
import { constant, GoldType } from '../constant';
import { GameApp } from '../GameApp';
import { UserData } from '../userData/UserData';
const { ccclass, property } = _decorator;


/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */
@ccclass('shop_build_UICtrl')
export class shop_build_UICtrl extends UIComponent {
    private scrollView: ScrollView = null
    private buildData: any = {}

    private coinNum = null
    private oreNum = null
    upCoin(name: string, data) {
        this.coinNum.string = UserData.Instance.localData["gold"]
        this.oreNum.string = UserData.Instance.localData["ore"]


    }
    //  private goldKey: string = "gold"

    upItem(upLevelBtn, root, item, gold_type, key) {

        this.upCoin("", null)

        //    console.log("buyBtn", root);

        root.getChildByName("name").getComponent(Label).string = item["name"]
        root.getChildByName("ProgressBar").getChildByName("level").getComponent(Label).string = item["level"] + "/" + item["maxLevel"]
        root.getChildByName("str").getComponent(Label).string = item["str"]

        root.getChildByName("nullLabel").active = false

        switch (gold_type) {
            case GoldType.gold:

                //  upLevelBtn.getChildByName("video").active = false
                upLevelBtn.getChildByName("icon").active = true
                upLevelBtn.getChildByName("icon2").active = false
                upLevelBtn.getChildByName("gold").getComponent(Label).string = item["gold"]
                break;

            case GoldType.ore:
                //   upLevelBtn.getChildByName("video").active = false
                upLevelBtn.getChildByName("icon2").active = true
                upLevelBtn.getChildByName("icon").active = false
                upLevelBtn.getChildByName("gold").getComponent(Label).string = item["gold"]
                break;

            default:
                break;
        }


        root.getChildByName("ProgressBar").getComponent(ProgressBarComponent).progress = item["level"] / item["maxLevel"]

        if (item["level"] >= item["maxLevel"]) {
            //已经满级
            upLevelBtn.active = false
            root.getChildByName("nullLabel").getComponent(Label).string = "已满级"
            root.getChildByName("nullLabel").active = true
        } else {




            upLevelBtn.on("click", () => {

                if (UserData.Instance.localData[gold_type] >= item["gold"]) {
                    upLevelBtn.off("click")
                    UserData.Instance.localData["bulidData"][key] += 1
                    UserData.Instance.localData[gold_type] -= item["gold"]
                    UserData.Instance.saveLocalData()
                    SoundMgr.Instance.playSound(ResMgr.Instance.getAsset("Sounds", "levelUp"))
                    this.upItem(upLevelBtn, root, item, gold_type, key)

                    this.upCoin("", null)
                    EventMgr.Instance.Emit(constant.EVENT_NAME.upHomeTop, null)

                } else {

                    switch (gold_type) {
                        case GoldType.gold:
                            GameApp.Instance.show_tip("金币不足")
                            break;

                        case GoldType.ore:
                            GameApp.Instance.show_tip("宝石不足")
                            break;

                        default:
                            break;
                    }

                }


            })
        }
    }

    init() {

        this.buildData = constant.buildData//大表
        // console.log(this.buildData);
        //700 200
        let height: number = 0
        let posY: number = 250

        //用遍历删除节点池

        for (let key in this.buildData) {
            const item = this.buildData[key]
            const gold_type: GoldType = item["goldType"]//当前资源所需要的货币类型




            //    console.log("我的key:" + gold_type);
            const n: Node = poolManager.instance.getNode(ResMgr.Instance.getAsset("GUI", "item/buildItem"), this.scrollView.content)

            posY -= 250
            height += 250
            n.setPosition(0, posY, 0)

            this.scrollView.content.getComponent(UITransform).height = height




            const root: Node = n.getChildByName("bg")
            const upLevelBtn: Node = root.getChildByName("btn")
            const adBtn: Node = root.getChildByName("adBtn")




            const imageAsset = ResMgr.Instance.getAsset("GUI", "buiidIcon/" + key)

            const spriteFrame = new SpriteFrame();
            const texture = new Texture2D();
            texture.image = imageAsset;
            spriteFrame.texture = texture;



            root.getChildByName("iconBg").getChildByName("icon").getComponent(Sprite).spriteFrame = spriteFrame;



            this.upItem(upLevelBtn, root, item, gold_type, key)




        }
    }
    onDestroy() {
        EventMgr.Instance.RemoveEventListener(constant.EVENT_NAME.upShopbuildTop, this.upCoin, this)

    }
    onLoad() {
        EventMgr.Instance.AddEventListener(constant.EVENT_NAME.upShopbuildTop, this.upCoin, this)
        this.coinNum = this.ViewNode("cion/Label").getComponent(Label)
        this.oreNum = this.ViewNode("ore/Label").getComponent(Label)
        this.scrollView = this.ViewNode("ScrollView").getComponent(ScrollView)
        this.AddButtonListener("outBtn", this, () => {
            GameApp.Instance.isBack = false
            UIMgr.Instance.DestroyUIView(this.node.name)
        })

        this.init()


    }

    start() {

    }

    update(dt: number) {

    }
}


